PRODUCT_NAME=XBMCControl
SOURCES=\
	main.m \
	XBMCControlApplication.m \
	MusicView.m \
	XBMCHttpInterface.m \
	MediaItem.m \
	PlaybackControlView.m \
	IconManager.m \
	SettingsView.m \
	Settings.m \
	SectionMusicView.m \
	SectionVideosView.m \
	SectionViewBase.m \
	NowPlayingView.m \
	MediaQueueView.m \

OSTYPE = $(shell uname)
IPODADDRESS=192.168.1.205
SRCROOT=.
RESOURCES=./Resources
BUILT_PRODUCTS_DIR=.
CONFIGURATION_TEMP_DIR=./build
INFOPLIST_FILE=Info.plist

ifeq ($(OSTYPE),Darwin)
CC=arm-apple-darwin9-gcc
else
CC=arm-apple-darwin-gcc
endif

CFLAGS=-g -O2 -Wall

ifeq ($(OSTYPE),Darwin)
CFLAGS+= -I/var/include -march=armv6 -mcpu=arm1176jzf-s -DTARGET_OS_IPHONE
endif

LD=$(CC)
LDFLAGS=-lobjc -ObjC -framework CoreFoundation -framework Foundation -framework CoreGraphics -framework GraphicsServices -framework UIKit 

ifeq ($(OSTYPE),Darwin)
LDFLAGS += -march=armv6 -mcpu=arm1176jzf-s -L"/usr/lib" -F"/System/Library/Frameworks" -F"/System/Library/PrivateFrameworks" -lz -bind_at_load
endif

LDID=/usr/bin/ldid

WRAPPER_NAME=$(PRODUCT_NAME).app
EXECUTABLE_NAME=$(PRODUCT_NAME)
SOURCES_ABS=$(addprefix $(SRCROOT)/,$(SOURCES))
INFOPLIST_ABS=$(addprefix $(SRCROOT)/,$(INFOPLIST_FILE))
OBJECTS=\
	$(patsubst %.c,%.o,$(filter %.c,$(SOURCES))) \
	$(patsubst %.cc,%.o,$(filter %.cc,$(SOURCES))) \
	$(patsubst %.cpp,%.o,$(filter %.cpp,$(SOURCES))) \
	$(patsubst %.m,%.o,$(filter %.m,$(SOURCES))) \
	$(patsubst %.mm,%.o,$(filter %.mm,$(SOURCES)))
OBJECTS_ABS=$(addprefix $(CONFIGURATION_TEMP_DIR)/,$(OBJECTS))
APP_ABS=$(BUILT_PRODUCTS_DIR)/$(WRAPPER_NAME)
PRODUCT_ABS=$(APP_ABS)/$(EXECUTABLE_NAME)
ifeq ($(OSTYPE),Darwin)
SIGN=sign
endif
ifeq ($(OSTYPE),Darwin)
INSTALL_IPHONE=install_iphone20
else
INSTALL_IPHONE=install_iphone1x
endif

all: $(PRODUCT_ABS) $(SIGN)

$(PRODUCT_ABS): $(APP_ABS) $(OBJECTS_ABS) $(SRCROOT)/*.h
	$(LD) $(LDFLAGS) -o $(PRODUCT_ABS) $(OBJECTS_ABS)

sign: $(PRODUCT_ABS)
	$(LDID) -S $(PRODUCT_ABS)

$(APP_ABS): $(INFOPLIST_ABS)
	mkdir -p $(APP_ABS)
	cp $(INFOPLIST_ABS) $(APP_ABS)/
	cp $(SRCROOT)/$(RESOURCES)/*.* $(APP_ABS)/

$(CONFIGURATION_TEMP_DIR)/%.o: $(SRCROOT)/%.m
	mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

clean:
	rm -f $(OBJECTS_ABS)
	rm -rf $(APP_ABS)

install: all $(INSTALL_IPHONE)


install_iphone20:
	cp -r $(APP_ABS) /Applications

install_iphone1x:
	echo scp -r $(APP_ABS) root@$(IPODADDRESS):/Applications
	echo ssh root@$(IPODADDRESS) 'killall SpringBoard'
